package com.ruoyi.mixuebingcheng.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.mixuebingcheng.domain.bo.ViewCouponBo;
import com.ruoyi.mixuebingcheng.domain.vo.ViewCouponVo;
import com.ruoyi.mixuebingcheng.domain.ViewCoupon;
import com.ruoyi.mixuebingcheng.mapper.ViewCouponMapper;
import com.ruoyi.mixuebingcheng.service.IViewCouponService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * VIEWService业务层处理
 *
 * @author ruoyi
 * @date 2023-10-27
 */
@RequiredArgsConstructor
@Service
public class ViewCouponServiceImpl implements IViewCouponService {

    private final ViewCouponMapper baseMapper;

    /**
     * 查询VIEW
     */
    @Override
    public ViewCouponVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询VIEW列表
     */
    @Override
    public TableDataInfo<ViewCouponVo> queryPageList(ViewCouponBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ViewCoupon> lqw = buildQueryWrapper(bo);
        Page<ViewCouponVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询VIEW列表
     */
    @Override
    public List<ViewCouponVo> queryList(ViewCouponBo bo) {
        LambdaQueryWrapper<ViewCoupon> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ViewCoupon> buildQueryWrapper(ViewCouponBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ViewCoupon> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getCouponId() != null, ViewCoupon::getCouponId, bo.getCouponId());
        lqw.eq(bo.getUserId() != null, ViewCoupon::getUserId, bo.getUserId());
        lqw.like(StringUtils.isNotBlank(bo.getName()), ViewCoupon::getName, bo.getName());
        lqw.eq(bo.getEffectiveTime() != null, ViewCoupon::getEffectiveTime, bo.getEffectiveTime());
        lqw.eq(bo.getExpirationTime() != null, ViewCoupon::getExpirationTime, bo.getExpirationTime());
        lqw.eq(bo.getType() != null, ViewCoupon::getType, bo.getType());
        lqw.eq(bo.getValue() != null, ViewCoupon::getValue, bo.getValue());
        lqw.eq(bo.getIsDeleted() != null, ViewCoupon::getIsDeleted, bo.getIsDeleted());
        lqw.eq(bo.getStatus() != null, ViewCoupon::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增VIEW
     */
    @Override
    public Boolean insertByBo(ViewCouponBo bo) {
        ViewCoupon add = BeanUtil.toBean(bo, ViewCoupon.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改VIEW
     */
    @Override
    public Boolean updateByBo(ViewCouponBo bo) {
        ViewCoupon update = BeanUtil.toBean(bo, ViewCoupon.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ViewCoupon entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除VIEW
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
